//读取、加载列表
var UrlTable = DataURL + 'bank/activity/getActivityByDeptId?checkStatus=';
loadactivity();
function loadactivity() {
	$.ajax({
		type : "GET",
		url : UrlTable + "2",
		dataType : 'json',
		async : false,
		success : function(data) {
			if(checkTokenError(data)){
			TableData = data.content.data;
			activityTable(TableData);
			}
		}
	});
}
function activityTable(TableData) {
	if(TableData){
	TableData.sort(DataSortUp);
	var TableList = "";
	for (var i = 0; i < TableData.length; i++) {
		TableList += '<tr data-id=' + TableData[i].id + '>';
		TableList += '<td>'+TableData[i].id+'</td>';
		TableList += '<td><img alt="活动图片" class="PreviewPic" src="'+TableData[i].picturePath+'" /> <div class="list-width list-name">'+TableData[i].theme+'</div></td>';
		TableList += '<td>'+cheackData(TableData[i].strDateStart,TableData[i].strDateEnd)+'</td>';
		TableList += '<td>'+TableData[i].object+'</td>';
		TableList += '<td><div><span class="startTime">开始</span><span class="time">'+TableData[i].strDateStart+'</span></div><div>	<span class="endTime">结束</span><span class="time">'+TableData[i].strDateEnd+'</span>	</div></td>';
		TableList += '<td>' + TableData[i].addr		+ '</td>';
		TableList += '<td class="control-button"><div class="control-delete">删除</div><div class="control-edit">编辑</div><div class="control-statistics">统计</div></td>';
		TableList += '<td><div class="preview">预览</div></td>';
		TableList += '</tr>';
	}
	$('#ActivityTable').find('tbody').html(TableList);
	}
}
//列表ID排序
function DataSortUp(x, y) {
	return (x.id < y.id) ? 1 : -1

}
$('#MyTable').DataTable(
		{
			"searching" : false,
			"info" : false,
			"lengthChange" : false,
			"pageLength" : 5,
			"ordering" : false,
			language : {
				"sEmptyTable" : "没有数据！",
				"oPaginate" : {

					"sPrevious" : '<',
					"sNext" : '>',

				},
			}
		});
//检查活动是否过期
function cheackData(StartTime,EndTime){
	var day = new Date();
	var year = day.getFullYear();
	var month = day.getMonth() + 1; // 记得当前月是要+1的
	var dt = day.getDate();
	var today = month+"/"+dt+"/"+year;
	var d1 = new Date(StartTime.replace(/\-/g, "\/"));
	var d2 = new Date(EndTime.replace(/\-/g, "\/"));
	var d3 = new Date(today.replace(/\-/g, "\/"));
	if (d1>d3){	return '<span class="statu1">未开始</span>';	}
	if(d2 >=d3)  
	 {  	 return '<span class="statu2">进行中</span>';	 }
	else{return '<span class="statu3">已结束</span>';}
}

//跳转预览页
$("#ActivityTable .preview")
		.click(
				function() {
					var myid = $(this).parent().parent().attr("data-id");
					window.location.href = HostURL
							+ '/activityManager/PreviewActivity?id=' + myid;
				});
//跳转编辑页
$("#ActivityTable .control-edit")
.click(
		function() {
			var myid = $(this).parent().parent().attr("data-id");
			window.location.href = HostURL
					+ '/activityManager/editActivity?id=' + myid;
		});
//跳转统计页
$("#ActivityTable .control-statistics")
.click(
		function() {
			var myid = $(this).parent().parent().attr("data-id");
			window.location.href = HostURL
					+ '/activityManager/activityStatistics?id=' + myid;
		});

//提示删除这一列
$("#ActivityTable .control-delete").click(function() {
	var myrow = $(this);
	var myname = myrow.parent().parent().find(".list-name").text();
	var myid = myrow.parent().parent().attr("data-id");
	var UrlDelete=DataURL + 'bank/activity/deleteActivityById';
	var DeleteData={"id":myid };
	zxTips_classic({
		Head : false,		// 是否存在头部
		ContentText : "是否将 " + myname + " <span class='statu2'>删除</span>？",
		// 提示内容
		Foot : true,		// 是否拥有底部
		FootCancel : true,		// 是否拥有底部取消按钮
		FootEnter : true,		// 是否拥有底部确认按钮
		clickEnter : function() {
			$.ajax({
				type : "POST",
				url : UrlDelete,
				dataType : 'json',
				data : DeleteData,// 传递给服务器的参数
				success : function(data) {
					zxTips_classicClose();
					if(checkTokenError(data)){
					zxTips_loadingend();
					zxTips_short({
						txt : data.description
					});
					table.row(myrow.parents('tr')).remove().draw();
					}
				},
		        error:function(data){
		            console.log(data)
		            zxTips_short({
		    			txt : "系统错误"
		    			});
		        }
			});
		}
	});
		});
//删除这一列
function DeleteActivity(){
	
}



// 引用列表插件
var table = $('#ActivityTable').DataTable({
	"searching" : false,
	"info" : false,
	"lengthChange" : false,
	"pageLength" : 5,
	"ordering" : false,
	language : {
		"sEmptyTable" : "没有数据！",
		"oPaginate" : {

			"sPrevious" : '<',
			"sNext" : '>',
		},
	}
});

//切换列表页
$('#ActivityTable tbody').bind("DOMNodeInserted", function () {
	$('.PreviewPic').unbind("click"); //移除click
	$(".PreviewPic").click(function() {
		zxTips_picpreview({
			picUrl:$(this).attr("src"),
			id:"myid",
			myclass:"myclass",
		});
		});
} );


